const ResourceCategory = require("../model/ResourceCategoryModel");

class ResourceCategoryDao {
  async save(data) {
    const res = await ResourceCategory.create(data);
    return res.toJSON();
  }

  async delete(Id) {
    const res = await ResourceCategory.destroy({
      where: {
        Id,
      },
    });
    return true;
  }

  async getOneById(Id) {
    const res = await ResourceCategory.findByPk(Id);
    if (res?.dataValues) {
      return res.toJSON();
    }

    return null;
  }

  // 获取某资源的类别ids
  async getResourceCategoryIds(data) {
    const { ResourceId, ResourceType } = data;
    const result = await ResourceCategory.findAll({
      attributes: [CategoryId],
      where: {
        ResourceId,
        ResourceType,
      },
    });

    if (result) {
      return result.toJSON();
    }

    return [];
  }
}
module.exports = new ResourceCategoryDao();
